/* Adicionar comentario (usuario do site) */
package servlets; 

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.Comentario;
import beans.Imagem;
import dbclass.ComentarioDAO;
import dbclass.ComentarioJDBC;
import dbclass.ImagemDAO;
import dbclass.ImagemJDBC;

/**
 * Servlet implementation class ComentariosAdd
 */
public class ComentariosAdd extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ComentariosAdd() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		
		ComentarioDAO comentarioDAO = new ComentarioJDBC();
		
		String corpo = request.getParameter("corpo");
		Long id_noticia = (long)Integer.parseInt(request.getParameter("id_noticia"));
		Date data = new Date(System.currentTimeMillis());  
		SimpleDateFormat formatador = new SimpleDateFormat("yyyy-MM-dd");  
		String dt = formatador.format( data );  
		
		Comentario comm = new Comentario(0, corpo, dt, id_noticia);  
		
		try { 
				  
			comentarioDAO.insert(comm); 
			String id_n = id_noticia + "";
			String url="NoticiasVer?msg_sucesso=Comentario adicionado&id=" + id_n;   
 
			response.sendRedirect(url); 
		} catch (ClassNotFoundException e) {  
			// TODO Auto-generated catch block 
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
